""" DateNotifier person editing dialog """
from PyQt4.uic.Compiler.qtproxies import QtGui
from S3D.DateNotifier.UI.AddNewDialog import Ui_AddNewDialog
from PyQt4.QtGui import *
from S3D.DateNotifier import _QDate2Str
from S3D.DateNotifier.Person import Person

class AddNewDialog(QDialog):
    def __init__(self, parent, person = None):
        super(AddNewDialog, self).__init__(parent)
        self.ui = Ui_AddNewDialog()
        self.ui.setupUi(self)

        self.tagList = parent.getTagList() if parent else []

        completer = QCompleter()
        completer.setModel(QStringListModel(self.tagList))
        completer.setCompletionMode(QCompleter.InlineCompletion)
        self.ui.tags.setCompleter(completer)

        if person is not None:
            self.ui.name.setText(person.name)
            self.ui.date.setSelectedDate(person.getQDate())
            self.ui.tags.setText(', '.join(sorted(person.tags)))

    def onTagsChanged(self, text):
        tag = text.replace(',', ' ').split()
        if len(tag) == 0:
            return
        tag = tag[-1]

        for t in self.tagList:
            if t.startswith(tag):
                hint = text + t[len(tag):]
                self.ui.tags.completer().setModel(QStringListModel([hint]))
                return

        self.ui.tags.completer().setModel(QStringListModel(self.tagList))

    def accept(self):
        if len(self.ui.name.text()) == 0:
            return

        self.done(self.Accepted)

    def getPerson(self):
        return Person(self.ui.name.text(), _QDate2Str(self.ui.date.selectedDate()), self.ui.tags.text())

if __name__ == '__main__':
    import sys
    qApp = QApplication(sys.argv)

    dlg = AddNewDialog(None)
    dlg.exec_()